<?php
/**
 * Created by JetBrains PhpStorm.
 * User: scala
 * Date: 12-11-6
 * Time: 上午1:22
 * To change this template use File | Settings | File Templates.
 */
class AccessAction extends CommonAction
{

    function index(){
        if(!empty($_GET['role_id'])){
            $this->assign("role_id",$_GET['role_id']);
            $this->display();
        }else{
            $this->error("当前角色不存在了！");
        }
    }


    function queryAccess(){
        if(!empty($_POST['role_id'])){
            $access=M("Access");
            $data['role_id']=$_POST['role_id'];
            $list=$access->where($data)->select();
            $node=M("Node");
            $where["status"]=1;
            $allNodes=$node->where($where)->order("pid,sort asc")->select();
            $ajaxData=array();
            foreach($allNodes as $val){
                $item=array();
                $item["id"]=$val["id"];
                $item["pId"]=$val["pid"];
                $item["name"]=$val['title'];
                foreach($list as $itemVal){
                    if($val['id']==$itemVal['node_id']){
                        $item["checked"]=true;
                        if($val['level']!=3){
                            $item["open"]=true;
                            $item["halfCheck"]=true;
                        }else{
                            $item["halfCheck"]=false;
                        }
                        break;
                    }
                }
                $ajaxData[]=$item;
            }
            $this->ajaxReturn($ajaxData,"success",1);
        }
        $this->ajaxReturn("","加载失败!",0);
    }

    function insertAccess(){
        if(!empty($_POST['role_id'])){
            if(!empty($_POST['data'])){
                $access=M("Access");
                $node=M("Node");
                $nodesList=$_POST['data'];
                $access->where("role_id=".$_POST['role_id'])->delete();
                $data=array();
                foreach($nodesList as $value){
                    $data[]=$value['id'];
                }
                $map['id']=array("in",$data);
                $selectedNodes=$node->where($map)->select();
                foreach($selectedNodes as $value){
                    $inserdata=array();
                    $inserdata["role_id"]=$_POST["role_id"];
                    $inserdata['node_id']=$value['id'];
                    $inserdata['level']=$value['level'];
                    $inserdata["pid"]=$value["pid"];
                    $access->add($inserdata);
                }
                $this->ajaxReturn("","分配权限成功！",1);
            }else{
                $this->ajaxReturn("","没有选择分配的权限！",0);
            }
        }else{
            $this->ajaxReturn("","角色组ID不存在!",0);
        }
    }

}
